package com.teach_sys.serlvet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.teach_sys.bean.SC;
import com.teach_sys.service.SCService;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "SCServlet", value = "/SCServlet")
public class SCServlet extends HttpServlet {

    private final SCService scService = new SCService();

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (request.getParameter("action").equals("queryAllSC")) {
            queryAllSC(response);
        } else if (request.getParameter("action").equals("querySCBySno")) {
            querySCBySno(request, response);
        } else if (request.getParameter("action").equals("querySCByCno")) {
            querySCByCno(request, response);
        } else if (request.getParameter("action").equals("addSC")) {
            addSC(request, response);
        } else if (request.getParameter("action").equals("deleteSC")) {
            deleteSC(request, response);
        } else if (request.getParameter("action").equals("updateSC")) {
            updateSC(request, response);
        }
    }

    public void queryAllSC(HttpServletResponse response) throws IOException {
        List<SC> scList = scService.queryAllSC();
        String result = JSON.toJSONString(scList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void querySCBySno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sno = request.getParameter("Sno");
        List<SC> scList = scService.querySCBySno(sno);
        String result = JSON.toJSONString(scList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void querySCByCno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cno = request.getParameter("Cno");
        List<SC> scList = scService.querySCByCno(cno);
        String result = JSON.toJSONString(scList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void addSC(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SC sc = JSON.parseObject(request.getParameter("formObject"), SC.class);
        scService.addSC(sc);
    }

    public void deleteSC(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sno = request.getParameter("Sno");
        String cno = request.getParameter("Cno");
        JSONArray jsonArray_sno = JSON.parseArray(sno);
        JSONArray jsonArray_cno = JSON.parseArray(cno);
        List<String> deleteList_sno = jsonArray_sno.toJavaList(String.class);
        List<String> deleteList_cno = jsonArray_cno.toJavaList(String.class);
        for (int i = 0; i < deleteList_sno.size(); i++) {
            scService.deleteSCBySnoAndCno(deleteList_sno.get(i), deleteList_cno.get(i));
        }
    }

    public void updateSC(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SC sc = JSON.parseObject(request.getParameter("formObject"), SC.class);
        scService.updateSC(sc);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void destroy() {
    }
}